package com.foreknow.view;

import com.foreknow.model.Admin;
import com.foreknow.sercive.AdminService;
import com.foreknow.sercive.Impl.AdminServiceImpl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Register extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Register frame = new Register();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Register() {
        setTitle("Register");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("\u7528\u6237\u540D\uFF1A");
        label.setBounds(78, 49, 54, 15);
        contentPane.add(label);

        textField = new JTextField();
        textField.setBounds(142, 46, 172, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label_1 = new JLabel("   \u5BC6\u7801\uFF1A");
        label_1.setBounds(78, 124, 54, 15);
        contentPane.add(label_1);

        passwordField = new JPasswordField();
        passwordField.setBounds(142, 121, 172, 21);
        contentPane.add(passwordField);

        JButton button = new JButton("\u6CE8\u518C");
        button.setBounds(56, 177, 93, 23);
        contentPane.add(button);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AdminService service = new AdminServiceImpl();
                String name = textField.getText();
                String password = new String(passwordField.getPassword());
                Admin admin=new Admin();
                admin.setAdminName(name);
                admin.setPassword(password);
                boolean isRight = service.add(admin);
                if (isRight){
                    Login login=new Login();
                    Register.this.dispose();
                }else{
                    JOptionPane.showMessageDialog(null, "添加失败", "错误",JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        JButton button_1 = new JButton("\u53D6\u6D88");
        button_1.setBounds(221, 177, 93, 23);
        contentPane.add(button_1);
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.setText("");
                passwordField.setText("");
            }
        });
        this.setVisible(true);
    }

}

